/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.dwr;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cq.base.domain.Page;
import com.cq.bd.base.BaseDwr;
import com.cq.bd.domain.SFunctionReso;
import com.cq.bd.domain.SMenu;
import com.cq.bd.domain.SRoleFunction;
import com.cq.bd.domain.SWorkerFunction;
import com.cq.bd.domain.SWorkerRole;
import com.cq.bd.security.UserDetails;
import com.cq.bd.service.FuncService;
import com.cq.bd.service.MenuService;
import com.cq.bd.service.RoleService;
import com.cq.bd.service.WorkerService;
import com.cq.bd.utils.AuthenticationHelper;

/**
 * 菜单管理
 * 
 * @author huangmy
 * @date   2009-10-5
 */
public class MenuDwr extends BaseDwr {
    private static final long serialVersionUID = 1L;
    private MenuService menuService; // 菜单管理
    private FuncService funcService; // 权限
    private WorkerService workerService;
    private RoleService roleService;
    
    /**
     * 创建菜单
     * 
     * @param menu
     */
    public void createMenu(SMenu menu) {
        menu.setCreateTime(new Date());
        this.menuService.doCreateMenu(menu);
    }
    
    /**
     * 修改菜单状态
     * 
     * @param menuId
     * @param state
     */
    public void changeMenuState(Long menuId, int state) {
        this.menuService.doChangeMenuState(menuId, state);
    }
    
    /**
     * 修改菜单
     * 
     * @param menu
     */
    public void updateMenu(SMenu menu) {
        menu.setUpdateTime(new Date());
        this.menuService.doUpdateMenu(menu);
    }
    
    /**
     *  获取菜单
     * 
     * @param menuId
     * @return
     */
    public SMenu getMenu(Long menuId) {
        return this.menuService.doGetMenu(menuId);
    }
    
    /**
     * 删除菜单
     * 
     * @param menu
     */
    public void deleteMenu(Long menuId) {
        this.menuService.doDeleteMenu(menuId);
    }
    
    /**
     * 分页查询菜单
     * 
     * @param filter
     * @param like
     * @param sort
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page pageMenus(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo, int pageSize){
        return this.menuService.pageMenus(filter, like, sort, pageNo, pageSize);    
    }
    
    /**
     * 所有的非末端菜单
     * 
     * @return
     */
    public List<SMenu> getAllNotEndMenus() { 
        List<SMenu> allMenus = new ArrayList<SMenu>();
        List<SMenu> menus = this.menuService.doGetAllNotEndMenus();
        if (menus != null) {
            allMenus.addAll(menus);
        }
        
        SMenu m = new SMenu();
        m.setMenuId(10000L);
        m.setMenuName("根目录");
        allMenus.add(m);
        
        return allMenus;
    }
    
    /**
     * 获取菜单, 检查权限
     * 
     * @param upperMenuId  上级菜单ID, 0 为根目录
     * @return
     */
    public List<SMenu> getMenus(Long upperMenuId) {
        UserDetails ud = (UserDetails)AuthenticationHelper.getUser();
        Long loginId = ud.getLoginId(); // LOGIN_ID
        List<SMenu> funcMenus = new ArrayList<SMenu>();
        
        if (upperMenuId == null) {
            upperMenuId = 0L;
        }
        
        List<SMenu> menus = this.menuService.doGetMenus(upperMenuId);
        if (menus == null) {
            return funcMenus;
        }
        
        if ("ADMIN".equals(ud.getLoginCode())) {
            return menus; // ADMIN用户不需要校验权限
        }
        
        for (SMenu menu : menus) {
            long menuId = menu.getMenuId();
            List<SWorkerRole> roles = this.workerService.doGetWorkerRoles(loginId); // 对应角色
            List<SWorkerFunction> workerFuncs = this.workerService.doGetWorkerFuncs(loginId); // 对应权限加减
            
            // 1. 角色对应的权限
            if (roles != null) {                
                for (SWorkerRole role : roles) {
                    List<SRoleFunction> roleFuncs = this.roleService.doGetRoleFuncs(role.getRoleId());
                    if (roleFuncs != null) {
                        for (SRoleFunction roleFunc : roleFuncs) {
                            Long funcId = roleFunc.getFuncId();
                            boolean hasNotFunc = false;
                            // 判断该权限是否已经被减
                            if (workerFuncs != null) {
                                for (SWorkerFunction wf : workerFuncs) {
                                    if (wf.getState() == 1 && wf.getAction() == 0 
                                            && wf.getFuncId() == funcId) {
                                        // 已经减去
                                        hasNotFunc = true;
                                    }
                                }
                            }
                            
                            if (!hasNotFunc) { // 权限没有被减去        
                                List<SFunctionReso> funcResos = this.funcService.doGetFuncResos(funcId);
                                if (funcResos != null) {
                                    for (SFunctionReso funcReso : funcResos) {
                                        int resoType = funcReso.getResoType();
                                        long resoId = funcReso.getResoId();
                                        
                                        if (resoType == 1 && menuId == resoId) {
                                            this.addFuncMenu(funcMenus, menu); // 符合权限的菜单
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            // 2. 工号增加的权限
            if (workerFuncs != null) {
                for (SWorkerFunction wf : workerFuncs) {
                    if (wf.getState() == 1 && wf.getAction() == 1 ) {
                        // 增加的权限
                        Long funcId = wf.getFuncId();
                        List<SFunctionReso> funcResos = this.funcService.doGetFuncResos(funcId);
                        if (funcResos != null) {
                            for (SFunctionReso funcReso : funcResos) {
                                int resoType = funcReso.getResoType();
                                long resoId = funcReso.getResoId();
                                
                                if (resoType == 1 && menuId == resoId) {
                                    this.addFuncMenu(funcMenus, menu); // 符合权限的菜单
                                }
                            }
                        }
                    }
                }
            }
        }
        
        return funcMenus;
    }
    
    /**
     * 不重复的增加菜单
     * 
     * @param funcMenus
     * @param menu
     */
    private void addFuncMenu(List<SMenu> funcMenus, SMenu menu) {
        boolean has = false;
        for (SMenu m : funcMenus) {
            if (m.getMenuId().equals(menu.getMenuId())) {
                has = true;
            }
        }
        
        if (!has) {
            funcMenus.add(menu);
        }
    }
    
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }
    
    public void setFuncService(FuncService funcService) {
        this.funcService = funcService;
    }

    public void setWorkerService(WorkerService workerService) {
        this.workerService = workerService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
}
